/********************************************************************************************
 Class Name: NMRCreateOpportunity
 Author: Accenture
 Date: 04/18/2012
 Requirement/Project Name: NMR
 Requirement Description: Class for conversion of Lead to Account, Opportunity, and Contact.
 Version History:0.3 (Added logic to update the secondary owner)
***********************************************************************************************/
 
public class NMRCreateOpportunity{
  
    //retrieve the record type Ids for Lead and Opportunity
    public static Id nmrOpptyRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'NMR Opportunity');//Oppotunity RecordType
    public static Id nmrAccountRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'CSS Customer');//Account Recordtype
    public static Id nmrContactRecordTypeID = RecordTypeHelper.GetRecordTypeID('Contact', 'IOG Contact');//Contact Recordtype  
 
    /**
    * Method to convert a lead. It will create an Account, Opportunity, and Contact. 
    */
    public static String convertLead(Lead lead){
        
        List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
        Database.LeadConvert leadToConvert = new Database.LeadConvert();
        List<Account> accountList = new List<Account>();
        List<Opportunity> opptyList = new List<Opportunity>();
        List<Contact> contactList = new List<Contact>();

        lead = [select Id, Name, Customer_Id__c, Street, City, State, PostalCode, Phone,Business_Description__c, email, NMR_Additional_Phone_3__c,
                NMR_Sales_Rep__c, ownerid from Lead where Lead.Id =: lead.Id];
        
        List<Account> acc = [select Id, Name from Account where RecordTypeId =: nmrAccountRecordTypeID and CSS_CustomerID__c =: lead.Customer_Id__c limit 1];
        
        if(acc.size() > 0)
        {
            leadToConvert.setAccountId(acc[0].Id);    
        }
            
        
        leadToConvert.setLeadId(lead.Id);
        leadToConvert.setConvertedStatus('Closed');
        lcList.add(leadToConvert);    

        
        try{
           List<Database.LeadConvertResult> resultList = Database.convertLead(lcList);
           
           Database.LeadConvertResult result = resultList[0];
           if(result.isSuccess()){
               
               accountList = [select Id, Name from Account where Id =: result.getAccountId()]; 
               opptyList = [select Id, Name from Opportunity where Id =: result.getOpportunityId()];
               contactList = [Select Id, Name, FirstName, LastName from Contact where Id =: result.getContactId()];
               
               if(accountList.size() > 0){
                   accountList[0].CSS_CustomerID__c = lead.Customer_Id__c;
                   accountList[0].CSS_ListingTel__c = lead.Phone;           
                   accountList[0].Contact_Phone__c = lead.Phone;    
                   accountList[0].Type_of_Business__c = lead.Business_Description__c;
                   accountList[0].Primary_Email__c = lead.email;
                   accountList[0].Fax = lead.NMR_Additional_Phone_3__c;
                   accountList[0].RecordTypeId = nmrAccountRecordTypeID;
                   accountList[0].Business_Address__c = lead.Street;
                   accountList[0].City__c = lead.City;
                   accountList[0].State__c = lead.State;
                   accountList[0].Zip__c = lead.PostalCode;
                   accountList[0].OOR_Secondary_Account_Owner__c = lead.NMR_Sales_Rep__c;
                   update accountList;   
               }
               
               if(opptyList.size() > 0){    
                   opptyList[0].Customer_ID__c = lead.Customer_Id__c;
                   opptyList[0].RecordTypeId = nmrOpptyRecordTypeID;
                   opptyList[0].StageName = 'Stage 0 - Proposal Pending creation';
                   opptyList[0].Customer_ID__c = lead.Customer_Id__c;
                   opptyList[0].CloseDate = Date.Today()+30;
                   if(contactList.size() > 0){
                      opptyList[0].Contact_name__c =  contactList[0].Id;
                   }                     
                   if(NMRUtil.isEmpty(lead.NMR_Sales_Rep__c))
                       opptyList[0].Secondary_Owner__c = lead.ownerid;
                   else
                       opptyList[0].Secondary_Owner__c = lead.NMR_Sales_Rep__c;
                       
                   update opptyList;
               }
               
               if(contactList.size() > 0){
                    contactList[0].RecordTypeId = nmrContactRecordTypeID;
                    if(NMRUtil.isEmpty(contactList[0].FirstName) && contactList[0].LastName.equalsIgnoreCase('NA'))
                        delete contactList;
                    else
                        update contactList;
                }    
           }
           
        }catch (Exception ce) {
            system.debug('NMRCreateOpportunity: Error Converting: ' + ce.getMessage());
        }
        if(accountList.size() > 0) 
            return accountList[0].Id;
        else
            return '';
    }
}